package com.my.cloud.service.impl;

import com.my.cloud.apis.AccountFeignApi;
import com.my.cloud.apis.StorageFeignApi;
import com.my.cloud.entities.Order;
import com.my.cloud.mapper.OrderMapper;
import com.my.cloud.service.OrderService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/14 10:26
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    
    @Resource
    private OrderMapper orderMapper;
    
    @Resource
    private StorageFeignApi storageFeignApi;
    
    @Resource
    private AccountFeignApi accountFeignApi;
    
    @Override
    @GlobalTransactional(name = "create-order", rollbackFor = Exception.class)//AT
    public void create(Order order) {
        // xid全局事务id的检查
        String xid = RootContext.getXID();
        // 1.新建订单
        log.info("------------------开始新建订单:\t\txid:" + xid);
        // 订单新建时默认初始状态为0
        order.setStatus(0);
        int result = orderMapper.insertSelective(order);
        // 插入订单成功获得插入mysql的实体对象
        Order orderFromDB = null;
        if (result > 0){
            // 从mysql中查出刚插入的记录
            orderFromDB = orderMapper.selectOne(order);
            log.info("---------------->新建订单成功，orderFromDB info：" + orderFromDB);
            System.out.println();
            // 2.扣减积分
            log.info("-------> 订单微服务开始调用Storage库存，做扣减count");
            storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("-------> 订单微服务开始调用Storage库存，做扣减完成");
            System.out.println();
            // 扣减账户余额
            log.info("-------> 订单微服务开始调用Account账号，做扣减money");
            accountFeignApi.decrease(orderFromDB.getUserId(), orderFromDB.getMoney());
            log.info("-------> 订单微服务开始调用Account账号，做扣减完成");
            System.out.println();
            // 4. 修改订单状态
            // 将订单状态从零修改为1，表示已经完成
            log.info("-----> 修改订单状态");
            orderFromDB.setStatus(1);
            Example whereCondition = new Example(Order.class);
            Example.Criteria criteria = whereCondition.createCriteria();
            criteria.andEqualTo("userId", orderFromDB.getUserId())
                    .andEqualTo("status", 0);
            int updateResult = orderMapper.updateByExampleSelective(orderFromDB, whereCondition);
            log.info("-----> 修改订单完成" + updateResult);
            log.info("-----> orderFromDB info:" + orderFromDB);

        }
        System.out.println();
        log.info("------------------结束新建订单:\t\txid:" + xid);


    }
}
